/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.st.config.Constant;
import com.jeesite.modules.st.entity.StStockCheckHistory;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StStockCheckHistoryService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.StStockCheckBill;
import com.jeesite.modules.st.service.StStockCheckBillService;

import java.util.List;

/**
 * 库存检验单Controller
 *
 * @author HOHD
 * @version 2021-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stStockCheckBill")
public class StStockCheckBillController extends BaseController {

    @Autowired
    private StStockCheckBillService stStockCheckBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StStockCheckBill get(String id, boolean isNewRecord) {
        return stStockCheckBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stStockCheckBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StStockCheckBill stStockCheckBill, Model model) {
        model.addAttribute("stStockCheckBill", stStockCheckBill);
        return "modules/st/stStockCheckBillList";
    }

    @RequiresPermissions("st:stStockCheckBill:view")
    @RequestMapping(value = {"checkForm"})
    public String checkForm(StStockCheckBill stStockCheckBill, Model model) {
        return "modules/st/stStockCheckBillForm-check-form";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stStockCheckBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StStockCheckBill> listData(StStockCheckBill stStockCheckBill, HttpServletRequest request, HttpServletResponse response) {
        stStockCheckBill.setPage(new Page<>(request, response));
        stStockCheckBill.setIsDel(Constant.is_del.not_del);
        Page<StStockCheckBill> page = stStockCheckBillService.findPage(stStockCheckBill);
        return page;
    }

    @Autowired
    StStockCheckHistoryService stStockCheckHistoryService;

    @RequiresPermissions("st:stStockCheckBill:view")
    @RequestMapping(value = "check")
    @ResponseBody
    public AjaxJson check(StStockCheckBill stStockCheckBill, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        String id = stStockCheckBill.getId();
        Integer checkType = stStockCheckBill.getCheckType();
        String checkDesc = stStockCheckBill.getCheckDesc();
        StStockCheckBill t = new StStockCheckBill();
        t.idIn(id.split(","));
        List<StStockCheckBill> list = stStockCheckBillService.findList(t);
        for (StStockCheckBill stockCheckBill : list) {
            stockCheckBill.setCheckDesc(checkDesc);
            stockCheckBill.setCheckType(checkType);
            stStockCheckBillService.save(stockCheckBill);
            StStockCheckHistory stStockCheckHistory = new StStockCheckHistory();
            stStockCheckHistory.setBatchNumber(stockCheckBill.getBatchNumber());
            stStockCheckHistory.setCheckDesc(checkDesc);
            stStockCheckHistory.setCheckResult(checkType);
            stStockCheckHistory.setProductName(stockCheckBill.getProductName());
            stStockCheckHistory.setProductCode(stockCheckBill.getProductCode());
            User user = UserUtils.getUser();
            stStockCheckHistory.setCheckManCode(user.getUserCode());
            stStockCheckHistory.setCheckMan(user.getUserName());
            stStockCheckHistory.setParentId(stockCheckBill.getId());
            stStockCheckHistoryService.save(stStockCheckHistory);
        }
        return ajaxJson;
    }

    @RequestMapping(value = "isCheck")
    @ResponseBody
    public AjaxJson isCheck(String batchNumber) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        StStockCheckBill stStockCheckBill = new StStockCheckBill();
        stStockCheckBill.setBatchNumber(batchNumber);
        stStockCheckBill.setIsDel(Constant.is_del.not_del);
        List<StStockCheckBill> list = stStockCheckBillService.findList(stStockCheckBill);

        if (list.isEmpty()) {
            ajaxJson.code = -1;
            ajaxJson.data = "该批次号不存在";
            return ajaxJson;
        }else if(list.size()>1){
            ajaxJson.code = -1;
            ajaxJson.data = "数据有异常";
            return ajaxJson;
        }

        stStockCheckBill = list.get(0);
        Integer checkType = stStockCheckBill.getCheckType();
        if (checkType == 1) {
            ajaxJson.code = -1;
            ajaxJson.data = stStockCheckBill.getProductName() + "未检验";
        } else if (checkType == 3) {
            ajaxJson.code = -1;
            ajaxJson.data = stStockCheckBill.getProductName() + "不合格";
        }
        return ajaxJson;
    }


    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stStockCheckBill:view")
    @RequestMapping(value = "form")
    public String form(StStockCheckBill stStockCheckBill, Model model) {
        model.addAttribute("stStockCheckBill", stStockCheckBill);
        return "modules/st/stStockCheckBillForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stStockCheckBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StStockCheckBill stStockCheckBill) {
        stStockCheckBillService.save(stStockCheckBill);
        return renderResult(Global.TRUE, text("保存库存检验单成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stStockCheckBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StStockCheckBill stStockCheckBill) {
        stStockCheckBillService.delete(stStockCheckBill);
        return renderResult(Global.TRUE, text("删除库存检验单成功！"));
    }

}